from google.appengine.ext import webapp
from google.appengine.ext import db
import urllib
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

#applicant_key=None

#Applicant Data Model Classes
class ApplicantStub(db.Model):
    password = db.TextProperty() #required=True)
    first_name = db.TextProperty() #required=True)
    middle_initial = db.TextProperty()
    last_name = db.TextProperty() #required=True)
    cell_phone_number = db.PhoneNumberProperty() #required=True)
    email_address = db.EmailProperty()
    security_question_answer = db.TextProperty()
    email_or_text = db.TextProperty() #required=True)
    comments = db.TextProperty(indexed=False)

class AddressStub(db.Model):
	parent_entity = db.TextProperty() #ReferenceProperty(ApplicantStub)
	applicant_address = db.PostalAddressProperty()
	home_phone_number = db.PhoneNumberProperty()
	comments = db.TextProperty(indexed=False)
	
class VitalsStub(db.Model):
	parent_entity = db.TextProperty()
	SSN = db.TextProperty()
	DOB = db.DateProperty()
	comments = db.TextProperty(indexed=False)
	
class EducationStub(db.Model):
	parent_entity = db.TextProperty()
	educational_institution = db.TextProperty()
	educational_location = db.TextProperty()
	degree = db.TextProperty()
	last_attended = db.DateProperty()
	graduated = db.BooleanProperty()
	comments = db.TextProperty(indexed=False)
	
class EmploymentStub(db.Model):
	parent_entity = db.TextProperty()
	employer_name = db.TextProperty()
	employer_phone_number = db.PhoneNumberProperty()
	employer_location = db.PostalAddressProperty()
	employee_position = db.TextProperty()
	date_started = db.DateProperty()
	date_ended = db.DateProperty()
	current_position = db.BooleanProperty()
	comments = db.TextProperty(indexed=False)
	
#Constructors & Posting Of Data
class PostApplicantStub(webapp.RequestHandler):
    def post(self):
    	applicant = ApplicantStub( #Constructor
        password = self.request.get("txtPassword"),
        first_name = self.request.get("txtFirstName"),
        middle_initial = self.request.get("txtMiddleInitial"),
        last_name = self.request.get("txtLastName"),
        cell_phone_number = self.request.get("inPhoneNumber"),
        email_address = self.request.get("txtEmailAdd"),
        security_question_answer = (self.request.get("txtSecQuestion")+self.request.get("txtSecQuestionAnswer")),
        email_or_text = self.request.get("selMessage"),
        comments = self.request.get("txtComments"),
        key_name = self.request.get("inPhoneNumber"))
        pkey = applicant.put()
        xkey_name = urllib.urlencode({'pkey': pkey.name()})
#        applicant_key=str(applicant.key_name)
        self.redirect('/stylesheets/GetStarted2.html?%s' % (xkey_name))

class PostAddressStub(webapp.RequestHandler):
	def post(self):
		applicant_key_name = self.request.get('pkey')
		if not applicant_key_name:
			self.redirect('/stylesheets/GetStarted2.html')
		applicant_key = applicant_key_name
		address = AddressStub( #parent=applicant,
		parent_entity = applicant_key,
		applicant_address = ("Add :"+self.request.get("txtStreetAdd1")+", "+self.request.get("txtStreetAdd2")+", "+
		    				 self.request.get("txtCity")+", "+self.request.get("txtState")+", "+self.request.get("txtZip+4")),
		home_phone_number = self.request.get("txtCurHomePhone"),
		comments = self.request.get("txtComments"))
		address.put()
		self.redirect('stylesheets/GetStarted3.html')
            

# Show Home Page - Start Here
class HomePage(webapp.RequestHandler):
    def get(self):
		self.redirect("/stylesheets/Home.html")
           
def main():
    application = webapp.WSGIApplication(
    									[('/', HomePage),
    									 ('/firstfrm', PostApplicantStub),		# from GetStated1.html
    									 ('/secondfrm', PostAddressStub),		# from GetStated2.html
    									 ('/thirdfrm', PostApplicantStub),		# from GetStated3.html
    									 ('/fourthfrm', PostApplicantStub)],	# from GetStated4.html
    									   debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    main()